import cv2
import os
import matplotlib.pyplot as plt

h = w = 25  # 提取区域的长宽
img_path = "./home/pi/Documents/CuBiC/Vision/picture"
'''--------------------------------------------------------------------------------'''
x1, y1 = 155, 60
x2, y2 = 300, 60
x3, y3 = 450, 60
x4, y4 = 155, 200
x5, y5 = 300, 200  # 一个面中心色块
x6, y6 = 450, 200
x7, y7 = 155, 350
x8, y8 = 300, 350
x9, y9 = 450, 350
'''--------------------------------------------------------------------------------'''
x10, y10 = 155, 60
x11, y11 = 300, 60
x12, y12 = 450, 60
x13, y13 = 155, 200
x14, y14 = 300, 200  # 一个面中心色块
x15, y15 = 450, 200
x16, y16 = 155, 350
x17, y17 = 300, 350
x18, y18 = 450, 350
'''--------------------------------------------------------------------------------'''
x19, y19 = 125, 140
x20, y20 = 175, 115
x21, y21 = 250, 70
x22, y22 = 115, 225
x23, y23 = 175, 215  # 一个面中心色块
x24, y24 = 250, 180
x25, y25 = 110, 320
x26, y26 = 175, 315
x27, y27 = 250, 310

x28, y28 = 350, 70
x29, y29 = 410, 120
x30, y30 = 452, 155
x31, y31 = 350, 175
x32, y32 = 418, 215  # 一个面中心色块
x33, y33 = 462, 240
x34, y34 = 350, 320
x35, y35 = 420, 325
x36, y36 = 465, 330
'''--------------------------------------------------------------------------------'''
x37, y37 = 125, 140
x38, y38 = 175, 115
x39, y39 = 250, 70
x40, y40 = 115, 225
x41, y41 = 175, 215  # 一个面中心色块
x42, y42 = 250, 180
x43, y43 = 110, 320
x44, y44 = 175, 315
x45, y45 = 250, 310

x46, y46 = 350, 70
x47, y47 = 410, 120
x48, y48 = 452, 155
x49, y49 = 350, 175
x50, y50 = 418, 215  # 一个面中心色块
x51, y51 = 462, 240
x52, y52 = 350, 320
x53, y53 = 420, 325
x54, y54 = 465, 330
'''---------------------------------------------------------------------------------'''
X = [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25,
     x26, x27, x28, x29, x30, x31, x32, x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44, x45, x46, x47, x48,
     x49, x50, x51, x52, x53, x54]  # 存储左上角坐标x值的矩阵
Y = [y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18, y19, y20, y21, y22, y23, y24, y25,
     y26, y27, y28, y29, y30, y31, y32, y33, y34, y35, y36, y37, y38, y39, y40, y41, y42, y43, y44, y45, y46, y47, y48,
     y49, y50, y51, y52, y53, y54]  # 存储左上角坐标y值的矩阵


def cv2_show(name, a) -> None:
    cv2.namedWindow(name, 0)
    cv2.imshow(name, a)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def roi_extract(img_path) -> list:
    roi_s = []  # 用于储存提取的roi
    path_list = os.listdir(img_path)
    path_list.sort(key=lambda x: int(x.split('.')[0]))  # 对‘.’进行切片，并取列表的第一个值（左边的文件名）转化整数型
    for i, file in enumerate(path_list):
        file_path = os.path.join(img_path, file)
        img = cv2.imread(file_path)
        if i < 2:
            for x, y in zip(X[i*9:(i+1)*9], Y[i*9:(i+1)*9]):
                if x != 0 and y != 0:
                    roi = img[y:y+h, x:x+w]
                    roi_s.append(roi)
        else:
            for x, y in zip(X[(i-1)*18:i*18], Y[(i-1)*18:i*18]):
                if x != 0 and y != 0:
                    roi = img[y:y+h, x:x+w]
                    roi_s.append(roi)
    return roi_s


if __name__ == '__main__':
    roi_s = roi_extract(img_path)
